<template>
	<view>
		<view class="van_top">
			<van-nav-bar title="门店信息认证" left-arrow @click-left="onClickLeft" />
		</view>
		<view class="top-title">
			门店信息
		</view>
		<view class="form">
			<form @submit="onSubmit">
				<van-field :value="shopForm.name" required :name="shopForm.name" title-width="64px" size="large" label="门店名称" placeholder="请输入门店名称"
					@change="e => shopForm.name = e.detail" :maxlength="20" :readonly="isDetail">
				</van-field>
				<van-field :value="shopForm.region" required :name="shopForm.region" title-width="64px" size="large" label="省市区域" placeholder="请选择地址" readonly
					@click-input="showRegion">
				</van-field>
				<van-field :value="shopForm.address" required :name="shopForm.address" title-width="64px" size="large" label="详细地址" placeholder="请输入门店详细地址"
					@change="e =>shopForm.address = e.detail" :maxlength="40" :readonly="isDetail">
				</van-field>
				<van-field :value="shopForm.imgUrl" required :name="shopForm.imgUrl" title-width="64px" size="large" label="营业执照" class="upload-field">
					<template #input>
						<view>

						</view>
					</template>
				</van-field>
				<view class="upload">
					<van-uploader :file-list="fileList" @after-read="afterRead" max-count="1" @delete="deleteImg" :deletable="!isDetail" :disabled="isDetail">
						<view class="bg-upload" v-if="fileList.length == 0">
							<view class="pic">
								<image src="../../static/addShop/camera.png" mode="aspectFill"></image>
							</view>
						</view>
					</van-uploader>
				</view>
				<view class="btn-box">
					<button class="submit-btn" type="info" form-type="submit" v-if="!isDetail">
						提交信息
					</button>
				</view>
			</form>
		</view>
		<van-popup :show="show" position="bottom" custom-style="height: 40%" @close="show = false" close-on-click-overlay safe-area-inset-bottom>
			<van-area :value="shopForm.regionId" :area-list="areaList" title="门店地址" @cancel="show = false" @confirm="regionConfirm"
				:swipe-duration="2000"/>
		</van-popup>
	</view>
</template>

<script>
	import { reqAddShop, reqShopInfo, reqUpdateShopInfo } from '@/apis/user.js'
	import formRules from '@/utils/formRules';
	import uploadFile from '@/utils/uploadFile';
	import { areaList } from '@vant/area-data';
	export default {
		data() {
			return {
				areaList: areaList,
				id: "",
				shopForm: {
					name: "",
					regionId: "410102",
					region: "",
					address: "",
					imgUrl: ""
				},
				rules: {
					name: [{ required: true, message: '请输入门店名称' }],
					regionId: [{ required: true, message: '请选择地址' }],
					address: [{ required: true, message: '请输入详细地址' }],
					imgUrl: [{ required: true, message: '请上传营业执照' }],
				},
				fileList: [],
				show: false,
				isDetail: false,
				type: "",
			}
		},
		onLoad(params) {
			this.type = params.type;
			// 0 未绑定 1 审核通过 2 审核失败
			if (params.type == "1" || params.type == "2") {
				this.toGetShopInfo();
			}
		},
		methods: {
			onClickLeft() {
				uni.navigateBack();
			},
			showRegion() {
				if (this.isDetail) return;
				this.show = true;
			},
			regionConfirm(e) {
				let values = e.detail.values;
				let nameArr = [];
				let codeArr = [];
				values.forEach(item => {
					nameArr.push(item.name);
					codeArr.push(item.code);
				})
				this.shopForm.region = nameArr.join('/')
				this.shopForm.regionId = codeArr.join(',')
				this.show = false;
			},
			async afterRead(event) {
				const { file } = event.detail //获取所需要上传的文件列表
				console.log(file.url);
				let res = await uploadFile(file.url);
				if (res.code === 200) {
					this.shopForm.imgUrl = res.data;
					this.fileList.push({ url: res.data })
				}
			},
			deleteImg(e) {
				let index = e.detail.index;
				this.fileList.splice(index, 1);
				this.shopForm.imgUrl = "";
			},
			onSubmit() {
				let { rules, shopForm } = this;
				formRules(rules, shopForm, async (status) => {
					if (status) {
						let { name, regionId, imgUrl, address, region } = shopForm;
						if (this.type == "2") {
							let res = await reqUpdateShopInfo({ id: this.id, name, regionId, imgUrl, address, region });
							if (res.code === 200) {
								uni.utils.toast("提交成功", "success");
								setTimeout(()=>{
									uni.switchTab({url:'/pages/index/index'});
								},1000)
								
							}
							return ;
						}
						let res = await reqAddShop({ name, regionId, imgUrl, address, region });
						if (res.code === 200) {
							uni.utils.toast("提交成功", "success");
							setTimeout(()=>{
								uni.switchTab({url:'/pages/index/index'});
							},1000)
						}
					}
				})
			},
			async toGetShopInfo() {
				if (this.type == "1") this.isDetail = true;
				let res = await reqShopInfo();
				if (res.code === 200) {
					let { name, region, regionId, address, imgUrl, id } = res.data;
					this.shopForm = { name, region, regionId, address, imgUrl };
					this.id = id;
					this.fileList.push({ url: imgUrl })
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.top-title {
		margin-top: 48rpx;
		margin-left: 24rpx;
		font-weight: bold;
		font-size: 28rpx;
		color: #333333;
	}

	.upload-field {
		/deep/ .van-cell::after {
			border: none !important;
		}
	}

	.upload {
		padding-left: 30rpx;
	}

	.bg-upload {
		background-color: #f3f3f3;
		width: 360rpx;
		height: 240rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 12rpx;
		margin-bottom: 96rpx;

		.pic {
			width: 134rpx;
			height: 124rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	/deep/ .van-uploader__preview {
		width: 360rpx !important;
		height: 240rpx !important;
		margin-bottom: 96rpx;

		image {
			width: 100% !important;
			height: 100% !important;
		}
	}

	.btn-box {
		padding: 0 48rpx;

		.submit-btn {
			height: 60rpx;
			background: linear-gradient(90deg, #FF4F30 0%, #FF1642 100%);
			line-height: 60rpx;
			border-radius: 8rpx;
			color: #fff;
			font-size: 32rpx;
			font-weight: 500;
			letter-spacing: 1px;
		}
	}
</style>